package com.maserb.monitor.web.author;

import com.maserb.monitor.entity.Function;
import com.maserb.monitor.entity.RoleFunction;
import com.maserb.monitor.entity.UserEntity;
import com.maserb.monitor.entity.card.RechargeRecordEntity;
import com.maserb.monitor.repository.FunctionRepository;
import com.maserb.monitor.repository.RoleFunctionRepository;
import com.maserb.monitor.repository.schoolStudent.RechargeRecordRepository;
import com.maserb.monitor.service.author.AuthorService;
import com.maserb.monitor.service.author_manage.RoleService;
import com.maserb.monitor.service.remote.EcCardService;
import org.apache.shiro.web.session.HttpServletSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Controller
@RequestMapping("/author")
public class AuthorController {

    @Autowired
    AuthorService authorService;

    @Autowired
    FunctionRepository functionRepository;

    @Autowired
    RoleFunctionRepository roleFunctionRepository;

    @Autowired
    RechargeRecordRepository rechargeRecordRepository;

    @Autowired
    EcCardService ecCardService;

    @Autowired
    RoleService roleService;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String getLogin() {
        return "author/login";
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String getIndex() {
        return "author/index";
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test() {
        List<RechargeRecordEntity> rechargeList = rechargeRecordRepository.findUnSend();
        for(RechargeRecordEntity recharge : rechargeList) {
            ecCardService.doAddRechargeRecord(recharge);
        }
        return "author/index";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String postLogin(UserEntity user, HttpServletRequest request, RedirectAttributesModelMap modelMap) {
        try {
            authorService.login(user);
            UserEntity authorizedUser = authorService.getAuthorizedUser();
            HttpSession session = request.getSession(true);
            session.setMaxInactiveInterval(18000);
            session.setAttribute("user", authorizedUser);

            if (authorizedUser.getUserName().equals("admin")) {
                List<Function> allFunction = functionRepository.findByFunctionTypeOrderByFunctionOrder((short) 0);
                List<String> userFunctionIdList = new ArrayList<String>();
                for (Function fun : allFunction) {
                    if (fun.getTSFunction() != null)
                        userFunctionIdList.add(fun.getTSFunction().getId());
                }
                session.setAttribute("allFunction", allFunction);
                session.setAttribute("userFunctionIdList", userFunctionIdList);
                session.setAttribute("userFunction", allFunction);
            } else {
                List<Function> allFunction = functionRepository.findByFunctionTypeOrderByFunctionOrder((short) 0);
                List<RoleFunction> userRoleFunction = roleFunctionRepository.findByRole(roleService.findOne(authorizedUser.getRole()));
                List<Function> userFunction = new ArrayList<Function>();
                List<String> userFunctionIdList = new ArrayList<String>();
                for (RoleFunction fun : userRoleFunction) {
                    userFunctionIdList.add(fun.getTSFunction().getId());
                    userFunction.add(fun.getTSFunction());
                }
                userFunction.sort(new Comparator<Function>() {
                    @Override
                    public int compare(Function o1, Function o2) {
                        return o1.getFunctionOrder().compareTo(o2.getFunctionOrder());
                    }
                });
                session.setAttribute("allFunction", allFunction);
                session.setAttribute("userFunctionIdList", userFunctionIdList);
                session.setAttribute("userFunction", userFunction);
            }
            return "redirect:/author/index";
        } catch (Exception e) {
            modelMap.addFlashAttribute("error", "用户名或密码错误");
            return "redirect:/author/login";
        }
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpSession session) {
        authorService.logout();
        return "redirect:/author/login";
    }
}
